// 白名单
import rootList from './rootList';

function jsonToUrlParam(json) {
	return Object.keys(json).map(key => key + '=' + json[key]).join('&');
}

function urlParamToJson(urlParam) {
	let json = {};
	urlParam.trim().split('&').forEach(item => {
		let [key, value] = item.split('=');
		json[key] = value;
	});
	return json;
}
export default {
	navTo({
		url,
		data
	}) {
		var that = this;
		const token = wx.getStorageSync('flower2025-token');
		const user = wx.getStorageSync('currentUser');
		if (rootList.NaWhiteList.includes(url)) {
			wx.navigateTo({
				url: url,
			})
			return;
		}
		if (token && user) {
			// 手机是否绑定
			if (!user.userMobile) {
				wx.showToast({
					title: '请授权登录后继续.',
					icon: "none",
					duration: 1500,
					success(res) {
						wx.navigateTo({
							url: '/pages/login/login',
						})
					}
				})
				return;
			} else {
				if (data) {
					url += "?" + jsonToUrlParam(data);
				}
				console.log(url);
				wx.navigateTo({
					url: url
				})
			}
		} else {
			wx.showToast({
				title: '请登录后继续',
				icon: "none",
				success(res) {
					setTimeout(() => {
						wx.navigateTo({
							url: '/pages/login/login',
						})
					}, 1500)
				}
			});
		}
	}
}